package com.atguigui.leetcode;

import java.util.Arrays;

/**
 * 462.最少移动次数使数组元素相等 II
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/5/19 8:40
 */
public class P462MinimumMovesToEqualArrayElementsIi {
    public static void main(String[] args) {
        Solution solution = new P462MinimumMovesToEqualArrayElementsIi().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minMoves2(int[] nums) {
            //对数据进行排序
            Arrays.sort(nums);
            //排序后取中间值
            int n = nums.length, ret = 0, x = nums[n / 2];
            //计算总共的步数
            for (int i = 0; i < n; i++) {
                ret += Math.abs(nums[i] - x);
            }
            return ret;
        }
    }
}
